#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
//#include <stdio.h>
#include "inout.h"
#include "control.h"
#include "uart.h"
#include "enc28j60/netstack.h"
//#include "smartprintf.h"

#define BAUD 56700 //256000 //56700
#define COUNTLIMIT 60
#include <util/setbaud.h>

//static FILE mystdout = FDEV_SETUP_STREAM(send, NULL, _FDEV_SETUP_WRITE);

volatile uint8_t shortupdate = 0;
volatile uint8_t longupdate = 0;
//volatile uint8_t ti_counter = 0;

static void main_loop(void);// __attribute__((no_return)); 

int __attribute__((OS_main))
main(void)
{

	*delay[0] = 5;
	*delay[1] = 1;
		
	//config uart
	UBRRH = UBRRH_VALUE;
	UBRRL = UBRRL_VALUE;

	#if USE_2X
		UCSRA |= (1 << U2X);
	#else
		UCSRA &= ~(1 << U2X);
	#endif

	UCSRB = (1 << RXEN) | (1 << TXEN) | (1 << RXCIE);
	UCSRC = (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1);

	//config timer1(16Mhz)
	//  16000000 / 256 / 62500  = 1 Interrupt/s
	TCCR1B = (1 << WGM12);	 // CTC Mode
	TCCR1B |= (1 << CS12);	 // Prescaler 256
	OCR1A = 62500;			 // Timer Output CompareA Value 62500

	//config timer0: Prescaler 1024, overflow interrupt
	// 16000000 / 1024 / 256 = 61 Interrupts/s
	TCCR0 = (1 << CS02) | (1 << CS00); 

	//config timer2: Prescaler 1024, compare interrupt
	// 16000000 / 1024 = 15625
	// /252 = 62 ints/s 
	TCCR2 |= (1 << CS22) | (1 << CS21) | (1 << CS20) | (1 << WGM21);
	OCR2 = 62;
	

	TIMSK |= (1 << TOIE0) | (1 << OCIE1A) | (1 << OCIE2);

	

//	stdout = &mystdout;
	cls();
	setmode(MODE_NONE);

//	smartprintf(ESC"?25l");	//hide cursor	
	pstring(ESC);
	pstring("?25l");
	
	reprint = true;

	//at beginning print complete screen with iostats
//	p_menu();
	
	//enable global interrupts
	sei();	
	// call function for setting up io pins
	ioinit();
//	cls();
	net_init();
	

	main_loop();
}

/*****************************************************
 * endless mainloop for calling printing and 
 *  inputhandling functions
 * reprintrate is limited by longupdate and 
 *	shortupdate (changed by timer0ovl ISR)    
 *****************************************************/

void main_loop()
{
		for(;;)
		{
/*			getsequence();
			setmode(MODE_NONE);
			if(reprint)
			{
				if(shortupdate)
				{
					shortupdate = 0;
					p_menu();
				}
				if(longupdate)
				{
					longupdate = 0;
					p_menu();
				}
			}
*/			
//			net_init();
			net_handle();
		}

}


/*****************************************************
 * periodic counter for reprinting
 * COUNTLIMIT = 60 --> 1s
 *****************************************************/

ISR(TIMER0_OVF_vect)
{
	

}

/*****************************************************
 * puts received byte from USART into rxbuffer
 *****************************************************/

ISR(USART_RXC_vect)
{
	
	if(rxi > (RXBUFFSIZE -3))
	{
		rxbuffovfl = 1;
	}
	else
	{
		rxbuffer[rxi] = UDR;
		rxi++;			
	}
	 
}

/*****************************************************
 * simple clock
 *****************************************************/
ISR(TIMER1_COMPA_vect)
{	
	TCNT1 = 0;

	seconds++;
	
	if(seconds == 60)
	{
		seconds = 0;
		minutes++;

		if(minutes == 60)
		{
			minutes = 0;
			hours++;
			
			if(hours == 24)
			{
				hours = 0;
			}
		}			
	}
}

ISR(TIMER2_COMP_vect)
{
	TCNT0 = 0;
	static int shortcount = 0;
	static int longcount = 0;
	shortcount++;

	if(shortcount == COUNTLIMIT)
	{
		shortupdate = 1;
		shortcount = 0;
		longcount++;
	}
	if(longcount == 3)
	{	
		longupdate = 1;
		longcount = 0;
	}
}
